package com.small.nacos.common.task;

import com.alibaba.nacos.common.utils.ExceptionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author zhoujin
 * @Date 2022/11/12 18:52
 */
public abstract class NacosRunnable implements Runnable {

    protected static final Logger LOGGER = LoggerFactory.getLogger(NacosRunnable.class);

    protected volatile Boolean shutdown = false;

    @Override
    public void run() {
        if (!shutdown) {
            try {
                before();

                executeBody();

                after();
            } catch (Exception e) {
                LOGGER.error("this task execute has error : {}", ExceptionUtil.getStackTrace(e));
            } finally {
                finalExecute();
            }
        }
    }

    protected abstract void executeBody();

    protected void before() {

    }

    protected void after() {

    }

    protected void finalExecute() {

    }

    public void shutdown() {
        shutdown = true;
    }
}
